-- INCLUDES
VFS.Include("LuaRules/Gadgets/Includes/utilities.lua")
VFS.Include("LuaRules/Gadgets/Includes/messagetypes.lua")

-- CONSTANTS
local GAIA_TEAM_ID = Spring.GetGaiaTeamID()
local MAXGATHERUNITLIMIT = 3;
local UNITCOST = "unitcost"

-- SYNCED CONTROL
Echo              = Spring.Echo
MessageDispatcher = Spring.SendLuaUIMsg

-- MEMBERS
local teamLimits = {}
local gameError = false
local gathers

-- GADGET CODE
function gadget:GetInfo()
	return {
		name      = "Limit manager",
		desc      = "Keeps track of the teams unit costs and makes sure you don't exceed the max unit limit",
		author    = "Sunspot",
		date      = "2011-06-14",
		license   = "GNU GPL v2",
		layer     = -2,
		enabled   = false
	}
end

if (gadgetHandler:IsSyncedCode()) then	
	function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
		if(teamLimits[teamID] > MAXGATHERUNITLIMIT) then
			--teamLimits[]
		end
	end

	function gadget:UnitDestroyed(unitID, unitDefID, teamID, attackerID)
		if (unitDefID)
	end
	
	function gadget:AllowUnitCreation(unitDefID, builderID, builderTeam, x, y, z)
		-- if no gameError is detected go further
		if not gameError then
			-- check if the new costtotal won't exceed MAXUNITLIMIT
			local cost = getUnitCost(unitDefID)
			local newTotal = teamLimits[builderTeam+1] + cost
			if(newTotal <= MAXUNITLIMIT) then
				return true
			else
				Echo("Unit limit reached")
				return false
			end
		else
			Echo("game error detected building disabled to disable gameplay")
			return false
		end		
	end
else
-- UNSYNCED
end